////////////////////////////////////////////////////////////////////////////////
// Copyright 2006 CORE Solutions Limited
// All rights reserved.
// PROPRIETARY AND CONFIDENTIAL INFORMATION.
// Use is subject to license terms.
//
// FUNCTION ID      :   VENDOR
// AUTHOR           :   Ric Yik 
// DATE CREATE      :   2006/01/11
// DESCRIPTION      :   a DAO file for the function, VENDOR
//                  :   
//                  :   
//                  :   
//
// MODIFICATION HISTORY:
// DATE         AUTHOR          VERSION NO.         COMMENT
// -----------  --------------  ------------------  -------------------
// 2006/01/11   Ric Yik         1.0                 source is created.
//
//
//
////////////////////////////////////////////////////////////////////////////////

package com.erp.app.util.web;

import java.util.StringTokenizer;

import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;

/**
 * Originally WebValidator
 */
public class EmailValidator {

    public String[] parseEmailAddresses(String emailType, String address, ActionErrors errors) throws Exception {
        String[] sendAddress = null;
        if (address == null) {
            return null;
        }
        if (address.length() == 0) {
            return null;
        }
        try {
            StringTokenizer st = new StringTokenizer(address, ",");
            sendAddress = new String[st.countTokens()];
            int i = 0;
            while (st.hasMoreTokens()) {
                String tmpStr = st.nextToken();
                boolean isValid = isValidEmailAddr(emailType, tmpStr);
                if (isValid) {
                    sendAddress[i] = tmpStr;
                }
                i++;
            }
        } catch (Exception e) {
            errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.general", e.getMessage()));
            throw new Exception(e.getMessage());
        }
        return sendAddress;
    }

    public String[] parseEmailAddresses(String emailType, String[] address, ActionErrors errors) throws Exception {
        String tmp = address[0];
        String[] sendAddress = null;
        if (address == null) {
            return null;
        }
        if (tmp.length() == 0) {
            return null;
        }
        try {
            StringTokenizer st = new StringTokenizer(tmp, ",");
            sendAddress = new String[st.countTokens()];
            int i = 0;
            while (st.hasMoreTokens()) {
                String tmpStr = st.nextToken();
                boolean isValid = isValidEmailAddr(emailType, tmpStr);
                if (isValid) {
                    sendAddress[i] = tmpStr;
                }
                i++;
            }
        } catch (Exception e) {
            throw new Exception(e.getMessage());
        }
        return sendAddress;
    }

    public static boolean isValidEmailAddr(String emailType, String emailAddr) throws Exception {
        int ndxAt = emailAddr.indexOf("@");
        int ndxDot = emailAddr.indexOf(".");
        int lastNdxDot = emailAddr.lastIndexOf(".");
        String errorMsg = "";
        boolean isValid = false;
        final int minimumDomainLength = 1;
        final int minimumLastNdxDotLength = 1;
        try {
            if ((ndxDot < 0) || (ndxAt < 0)) {
                errorMsg = emailType + " email address lacks . or @";
                throw new Exception(errorMsg);
                // } else if(ndxDot < ndxAt) {
                // errorMsg = emailType + " email address entered a '.' before the '@'.";
                // throw new Exception(errorMsg);
            } else if ((lastNdxDot - minimumDomainLength) <= ndxAt) {
                // There exists email having the domain with less than 3 character. e.g. go@go.com
                // A change is done to change the minimumDomainLength from 3 into 1, which is the minimum valid domain length.
                errorMsg = emailType + " email address may be missing your domain name.";
                throw new Exception(errorMsg);
            } else if (emailAddr.length() < (lastNdxDot + minimumLastNdxDotLength)) {
                // There exists email having the last "suffix" with less than 4 character. e.g. core@core.com.hk
                // A change is done to change the minimumLastNdxDotLength from 4 into 1, which excludes all invalid "suffix".
                errorMsg = emailType + " email address have fewer than " + minimumLastNdxDotLength + " characters as a domain suffix.";
                throw new Exception(errorMsg);
            } else {
                isValid = true;
            }
        } catch (Exception e) {
            throw new Exception(e.getMessage());
        }
        return isValid;
    }

    // public static void main(String[] args){
    // try{
    // System.out.println("BAD:");
    // System.out.println(isValidEmailAddr("", "victordfds dfad fdsfs"));
    // System.out.println(isValidEmailAddr("", ".victor@this.com"));
    // System.out.println(isValidEmailAddr("", "victor@this"));
    // System.out.println(isValidEmailAddr("", "victor@th."));
    // System.out.println(isValidEmailAddr("", "victor@this."));
    // System.out.println(isValidEmailAddr("", "victor@th.cn"));
    // System.out.println("GOOD:");
    // System.out.println(isValidEmailAddr("", "victor@the.com"));
    // System.out.println(isValidEmailAddr("", "victor@the.info"));
    // System.out.println(isValidEmailAddr("", "victor@this.co.uk"));
    // }catch(Exception e){
    // e.printStackTrace();
    // }
    // }

}
